<!DOCTYPE html>
<html>
	<head lang="en">
		<meta charset="utf-8" />
		<title>最家</title>
		<link rel="stylesheet" type="text/css" href="css/public.css"/>
		<link rel="stylesheet" type="text/css" href="css/mygxin.css" />
	</head>
	<body>
		<!------------------------------head------------------------------>
		<div th:replace="headbing.html"></div>
		<!------------------------------idea------------------------------>
		<div class="address mt">
			<div class="wrapper clearfix">
				<a href="index.html" class="fl">首页</a>
				<span>/</span>
				<a onclick="javaScript:personal()">个人中心</a>
				<span>/</span>
				<a href="address.html" class="on">地址管理</a>
			</div>
		</div>
		
		<!------------------------------Bott------------------------------>
		<div class="Bott">
			<div class="wrapper clearfix">
				<div th:replace="bott.html"></div>
				<div class="you fl">
					<h2>收货地址</h2>
					<div class="add">
						<div>
							<a href="#2" id="addxad"><img src="img/jia.png"/></a>
							<span>添加新地址</span>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--编辑弹框-->
		<!--遮罩-->
		<div class="mask"></div>
		<div class="adddz">
			<form action="#" method="get">
				<input type="text" placeholder="姓名" class="on1" />
				<input type="text" placeholder="手机号" class="phone" />
				<div class="city" id="demo4">
					<select id="province" name="province">
						<option  value="省份/自治区">省份/自治区</option>
					</select>
					<select id="city" name="city">
						<option value="城市/地区">城市/地区</option>
					</select>
					<select id="area" name="area">
						<option value="区/县">区/县</option>
					</select>
					<select id="town" name="town">
						<option value="街道">配送区域</option>
					</select>
				</div>
				<textarea name="detailed" rows="" cols="" placeholder="详细地址"></textarea>
				<div class="bc">
					<input type="button" onclick="javaScript:site()" value="保存" />
					<input type="button" value="取消" />
				</div>
			</form>
		</div>
		<div class="readd">
			<form action="#" method="get">
				<input type="text" id="bape" class="on" value="" />
				<input type="text" id="phone"  value="157****0022" />
				<div class="city" id="demo3">

					<select id="province2" name="province">
						<option  value="省份/自治区">省份/自治区</option>
					</select>
					<select id="city2" name="city">
						<option value="城市/地区">城市/地区</option>
					</select>
					<select id="area2" name="area">
						<option value="区/县">区/县</option>
					</select>
				</div>
				<textarea name="" rows="" cols="" id="xx" placeholder="详细地址">高新产业园</textarea>
				<div class="bc">
					<input type="button" onclick="save()" value="保存" />
					<input type="button" onclick="javaScript:cancel()" value="取消" />
				</div>
			</form>
		</div>
		<!--返回顶部-->
		<div th:replace="gotop.html"></div>
		<!--footer-->
		<div class="footer">
			<div class="top">
				<div class="wrapper">
					<div class="clearfix">
						<a href="#2" class="fl"><img src="img/foot1.png"/></a>
						<span class="fl">7天无理由退货</span>
					</div>
					<div class="clearfix">
						<a href="#2" class="fl"><img src="img/foot2.png"/></a>
						<span class="fl">15天免费换货</span>
					</div>
					<div class="clearfix">
						<a href="#2" class="fl"><img src="img/foot3.png"/></a>
						<span class="fl">满599包邮</span>
					</div>
					<div class="clearfix">
						<a href="#2" class="fl"><img src="img/foot4.png"/></a>
						<span class="fl">手机特色服务</span>
					</div>
				</div>
			</div>
			<p class="dibu">最家家居&copy;2013-2017公司版权所有 京ICP备080100-44备0000111000号<br />
			违法和不良信息举报电话：400-800-8200，本网站所列数据，除特殊说明，所有数据均出自我司实验室测试</p>
		</div>
		<script src="js/jquery-1.12.4.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/public.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/user.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="js/jquery.min.js" ></script>
		<script type="text/javascript" src="js/jquery.citys.js"></script>
		<script type="text/javascript">
			var $town = $('#demo3 select[name="town"]');
			var townFormat = function(info){
				$town.hide().empty();
				if(info['code']%1e4&&info['code']<7e5){	//是否为“区”且不是港澳台地区
					$.ajax({
						url:'http://passer-by.com/data_location/town/'+info['code']+'.json',
						dataType:'json',
						success:function(town){
							$town.show();
							for(i in town){
								$town.append('<option value="'+i+'">'+town[i]+'</option>');
							}
						}
					});
				}
			};
			$('#demo3').citys({
				province:'福建',
				city:'厦门',
				area:'思明',
				onChange:function(info){
					townFormat(info);
				}
			},function(api){
				var info = api.getInfo();
				townFormat(info);
			});
		</script>
		<script type="text/javascript">
			var $town = $('#demo4 select[name="town"]');
			var townFormat = function(info){
				$town.hide().empty();
				if(info['code']%1e4&&info['code']<7e5){	//是否为“区”且不是港澳台地区
					$.ajax({
						url:'http://passer-by.com/data_location/town/'+info['code']+'.json',
						dataType:'json',
						success:function(town){
							$town.show();
							for(i in town){
								$town.append('<option value="'+i+'">'+town[i]+'</option>');
							}
						}
					});
				}
			};
			$('#demo4').citys({
				province:'福建',
				city:'厦门',
				area:'思明',
				onChange:function(info){
					townFormat(info);
				}
			},function(api){
				var info = api.getInfo();
				townFormat(info);
			});
		</script>
	<script>
		function site() {
			//电话正则表达式
			var pattern = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
			//收货人姓名
			 var consignee=$(".on1").val();
			//收货人电话
			 var phone=$(".phone").val();
			//收货人省市区街道
			 var province=$("#province option:selected").text();
			 var city=$("#city option:selected").text();
			 var area=$("#area option:selected").text();
			 var detailed=$("#town option:selected").text();
			 //收货人详细地址
			var address=$("[name='detailed']").val();
			var userId=$(".zuo h3 .f2").text();
			if (consignee==""||consignee==null){
				alert("收货人不能为空")
				return false;
			}
			if (phone==""||phone==null){
				alert("电话不能为空")
				return false;
			}
			if(address==""||address==null){
				alert("详细地址不能为空")
				return false;
			}
			if(pattern.test(phone)==false){
				alert("收货电话不对劲")
				return false;

			}
				if (userId==""||userId==null){
				alert("请先登录")
				location.href = "login.html";
				return false;

			}
			$.ajax({
				url:"http://localhost:8080/site",
				type:"post",
				data:{"consignee":consignee,"phone":phone,"province":province,"city":city,"area":area,"detailed":detailed,"address":address,"userId":userId},
				success:function(site) {
					if (site.success) {
						var data =site.data;
						if (data>0) {
							alert("新增地址成功")
						} else {
							alert("地址错误")
						}
						location.href = "address.html";
					}
				},
				error: function () {
					console.log("出现未知错误")
				}
			})
		}
	</script>
		<script>
				var userId=$(".zuo h3 .f2").text();
				$.ajax({
					url:"http://localhost:8080/address2.html",
					data:{"userId":userId},
					success:function (d) {
						$.each(d,function (k,v) {
							$(".add").append("<div id='dizhi"+v.id+"'>" +
									"<p>"+v.consignee+"</p>" +
									"<p>"+v.phone+"</p>" +
									"<p>"+v.province+"&nbsp;&nbsp;"+v.city+"&nbsp;"+v.area+"</p>" +
									"<p>"+v.detailed+"&nbsp;"+v.address+"</p>" +
									"<p ><a href='javaScript:void(0)' onclick='update("+v.id+")'>修改</a>&nbsp<a href='javaScript:void(0)' onclick='del("+v.id+")'>删除</a></p>"+
									"</div>")
						})

					}
				})
				function del(id) {
					$.ajax({
					url:"http://localhost:8080/delid/"+id,
					success:function (resp) {
						if (resp.success){
							var count=resp.data;
							if (count>0){
								$("#dizhi"+id).remove()
							}
							}else {

						}

					},
					});
				}
		</script>
	<script>
		var pid;
		function update(id) {

			pid = id;
			//收货人
			var closed=$("#dizhi"+id+" p:eq(0)" ).text();
			//电话
			var poh=$("#dizhi"+id+" p:eq(1)" ).text();
			//配送地址
			var address=$("#dizhi"+id+" p:eq(3)" ).text();
			$("#bape").val(closed);
			$("#phone").val(poh);
			$("#xx").val(address);
			$(".readd").show();
		}
		function cancel() {
			$(".readd").hide();

		}
		function save() {
			var pattern = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
			//姓名
			var a1=$("#bape").val();
			//电话
			var a2=$("#phone").val();
			//省市区街道
			var a3=$("#province2 r").text();
			var a4=$("#city2 option:selected").text();
			var a5=$("#area2 option:selected").text();
			var a6=$("#town2 option:selected").text();
			//详细地址
			var a7=$("#xx").val();
			if (pattern.test(a2)==false){
				alert("电话号码不对劲")
				return false;
			}
			$.ajax({
				url:"http://localhost:8080/update",
				data:{
					"a1":a1,
					"a2":a2,
					"a3":a3,
					"a4":a4,
					"a5":a5,
					"a6":a6,
					"a7":a7,
					"pid":pid
				},
				success:function (resp) {
					if (resp.success){
						var count=resp.data;
						if (count>0){
							location.href = "address.html";
						}
					}else {

					}

				},
			})

		}
	</script>
	</body>

</html>
